﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;
using Reborn_WorldServer.App1AppHandler.Spells.Interfaces;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_ArmorBreak : Buff, IInstantCast
    {
        private int ArmorRemoved = 20;

        public Spell_ArmorBreak()
        {
            minutesLeft = 3;
        }


        protected int calculateAdds()
        {
            return SkillManager.AdjustForCombat(CastingCreatureLevel / 2, ArmorRemoved, CastingCreatureInt / 2);
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            if (timeRemaining != -1)
                minutesLeft = timeRemaining;

            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            ArmorRemoved = calculateAdds();

            TargetCreatureBase.Armor = TargetCreatureBase.Armor - ArmorRemoved;
            TargetCreatureBase.Update_Character();

            spellTimer.Start();
        }

        override public bool tryResist(SpellIcon _spellIcon, int castLevel, int castInt, int castWis, CreatureBase targetCreatureBase)
        {
            int penetration = SkillManager.AdjustForCombat(castLevel, 0, castInt);

            int resist = targetCreatureBase.getResist(_spellIcon.Skill);

            int result = Managers.GameHandler.random.Next(1, penetration + resist + 1);
            if (result > penetration)
                return true;

            return false;
        }

        public void InstantCast(CreatureBase castingCreature, CreatureBase targetCreatureBase, SpellIcon _spellIcon, int castLevel)
        {
            if (base.InitializeInstantCast(castingCreature, targetCreatureBase, _spellIcon, castLevel, true) == false)
                return;

            ArmorRemoved = calculateAdds();

            TargetCreatureBase.Armor = TargetCreatureBase.Armor - ArmorRemoved;

            TargetCreatureBase.buffManager.UpdateBuffs();

            foreach (MapItem mapitem in TargetCreatureBase.currentMapItem.mapItemsWithin4)
            {
                if (Math.Abs(mapitem.MapX - targetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - targetCreatureBase.MapY) <= 4)
                {
                    foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
                    {
                        if (creatureBase is Character)
                        {
                            Character charToUpdate = (Character)creatureBase;
                            charToUpdate.AddCreatureBase(targetCreatureBase);
                        }
                    }
                }
            }

            spellTimer.Start();
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            ArmorRemoved = calculateAdds();

            TargetCreatureBase.Armor = TargetCreatureBase.Armor - ArmorRemoved;
            TargetCreatureBase.Update_Character();
            
            TargetCreatureBase.buffManager.UpdateBuffs();


            //Start Spell Timer
            spellTimer.Start();
        }
        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);

            TargetCreatureBase.Armor = TargetCreatureBase.Armor + ArmorRemoved;
            TargetCreatureBase.Update_Character();
        }
    }
}
